function [ofv] = nopt()

%load remaining data
data_struct = load('data_nopt.mat');
segNP = double(data_struct.segNP);
Y = data_struct.Y;
cap = double(data_struct.cap);
eps = data_struct.eps;
bound = data_struct.bound;
upMat = data_struct.upMat;
os = double(data_struct.os);
uniqSeg = double(data_struct.uniqSeg);
nyp = double(data_struct.nyp);
prices = double(data_struct.prices);
capcon = double(data_struct.capcon);
segScale = double(data_struct.segScale);
segQ = double(data_struct.segQ);
rem_cap = double(data_struct.rem_cap);
sharepairs = double(data_struct.sharepairs);
szpairs = double(data_struct.szpairs);
uniqSeg_size = size(uniqSeg);
nNewSegments = uniqSeg_size(1);
prices_size = size(prices);
nProducts = prices_size(1);
[y1,y2,y3]=ind2sub(size(upMat),find(upMat));
nRes = length(rem_cap);
inst = double(data_struct.inst);
einpreis = uniqSeg(:,4);

prices_lb = zeros(nProducts,nProducts,nNewSegments);
for i=1:nNewSegments
     prices_lb(y1(i),y2(i),i) = prices(y1(i));
end

u = zeros(nNewSegments,3+nProducts);
usub = zeros(nNewSegments,nProducts);
for i = 1:nNewSegments
    u(i,:) = [uniqSeg(i,1)+1,y1(i),y2(i),os(uniqSeg(i,3)+1,:)];
    usub(i,:) = os(uniqSeg(i,3)+1,:);
    usub(i,y2(i)) = 0;
end

%Matrix A
anzNBcap = find(rem_cap~=0,1,'last')-1; 
anzNByp = nNewSegments;
anzEV = nNewSegments;
A = zeros(anzNBcap+anzNByp,anzEV);
for i = 1:anzNBcap
    h = i+1; 
    for n = 1:nNewSegments
        A(i,n) = (capcon(y1(n),i)-capcon(y1(n),h));
    end
end
A(anzNBcap+1:anzNBcap+anzNByp,1:nNewSegments) = diag(ones(nNewSegments,1));

%b, Aeq, beq
for i = 1:nNewSegments
        probs(i) = (1-sharepairs)*max(0,1-((exp(segNP(u(i,1)))+u(i,4)*exp(1/segScale(u(i,1))*(segQ(1,u(i,1))-prices(1)))...
                +u(i,5)*exp(1/segScale(u(i,1))*(segQ(2,u(i,1))-prices(2)))+u(i,6)*exp(1/segScale(u(i,1))*(segQ(3,u(i,1))-prices(3)))...
                +u(i,7)*exp(1/segScale(u(i,1))*(segQ(4,u(i,1))-prices(4)))+u(i,8)*exp(1/segScale(u(i,1))*(segQ(5,u(i,1))-prices(5)))...
                +u(i,9)*exp(1/segScale(u(i,1))*(segQ(6,u(i,1))-prices(6))))/...
                (exp(1/segScale(u(i,1))*(segQ(u(i,3),u(i,1))-einpreis(i)))+exp(segNP(u(i,1)))+usub(i,1)*exp(1/segScale(u(i,1))*(segQ(1,u(i,1))-prices(1)))...
                +usub(i,2)*exp(1/segScale(u(i,1))*(segQ(2,u(i,1))-prices(2)))+usub(i,3)*exp(1/segScale(u(i,1))*(segQ(3,u(i,1))-prices(3)))...
                +usub(i,4)*exp(1/segScale(u(i,1))*(segQ(4,u(i,1))-prices(4)))+usub(i,5)*exp(1/segScale(u(i,1))*(segQ(5,u(i,1))-prices(5)))...
                +usub(i,6)*exp(1/segScale(u(i,1))*(segQ(6,u(i,1))-prices(6))))))...
                +sharepairs*max(0,(1-((exp(segNP(u(i,1)))+u(i,4)*exp(1/segScale(u(i,1))*(segQ(1,u(i,1))-prices(1)))...
                +u(i,5)*exp(1/segScale(u(i,1))*(segQ(2,u(i,1))-prices(2)))+u(i,6)*exp(1/segScale(u(i,1))*(segQ(3,u(i,1))-prices(3)))...
                +u(i,7)*exp(1/segScale(u(i,1))*(segQ(4,u(i,1))-prices(4)))+u(i,8)*exp(1/segScale(u(i,1))*(segQ(5,u(i,1))-prices(5)))...
                +u(i,9)*exp(1/segScale(u(i,1))*(segQ(6,u(i,1))-prices(6))))/...
                (exp(1/segScale(u(i,1))*(segQ(u(i,3),u(i,1))-einpreis(i)))+exp(segNP(u(i,1)))+usub(i,1)*exp(1/segScale(u(i,1))*(segQ(1,u(i,1))-prices(1)))...
                +usub(i,2)*exp(1/segScale(u(i,1))*(segQ(2,u(i,1))-prices(2)))+usub(i,3)*exp(1/segScale(u(i,1))*(segQ(3,u(i,1))-prices(3)))...
                +usub(i,4)*exp(1/segScale(u(i,1))*(segQ(4,u(i,1))-prices(4)))+usub(i,5)*exp(1/segScale(u(i,1))*(segQ(5,u(i,1))-prices(5)))...
                +usub(i,6)*exp(1/segScale(u(i,1))*(segQ(6,u(i,1))-prices(6))))))^szpairs);
end
b = [rem_cap(2:anzNBcap+1)';Y(sub2ind(size(Y),y1,y2,y3)).*probs'];
Aeq = [];
beq = [];

%OF
of = @of_nopt;

[~,q2] = find(capcon~=0);
x0 = [zeros(nNewSegments,1)];

lb = [zeros(nNewSegments,1)];
ub = [Y(sub2ind(size(Y),y1,y2,y3))];

% fmincon
options = optimoptions('fmincon','MaxIterations',5000,'MaxFunctionEvaluations',30000);

tic;
[x,ofv,ef] = fmincon(of,x0,A,b,Aeq,beq,lb,ub,[],options);
tsub = toc;  